package com.tuanzi.loan.business.repository.receipt.statistics.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.repository.receipt.statistics.custom.ReceiptRechargeStatisticsRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.receipt.statistics.ReceiptRechargeStatisticsSearchRequest;
import com.tuanzi.loan.vo.response.receipt.statistics.ReceiptRechargeStatisticsSearchResponse;
import com.tuanzi.server.loan_base.constant.LoanReceiptProductStatus;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.math.BigDecimal;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanReceiptProductInfo.LOAN_RECEIPT_PRODUCT_INFO;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanReceiptRechargeRecord.LOAN_RECEIPT_RECHARGE_RECORD;



/**
 * @author lsx
 */
public class ReceiptRechargeStatisticsRepositoryImpl extends JOOQBaseRepositoryPlus implements ReceiptRechargeStatisticsRepositoryCustom {

    @Override
    protected TableImpl getTable() {
        return LOAN_RECEIPT_RECHARGE_RECORD;
    }
    

    @Override
    public Page<ReceiptRechargeStatisticsSearchResponse> list(ReceiptRechargeStatisticsSearchRequest searchRequest, Pageable pageable) {
        fields(LOAN_RECEIPT_PRODUCT_INFO.NAME,LOAN_RECEIPT_PRODUCT_INFO.ID);

        if (searchRequest.getReceiptProductId()== null || searchRequest.getSourceFrom() == null){
            fields(LOAN_RECEIPT_RECHARGE_RECORD.DATE, LOAN_RECEIPT_RECHARGE_RECORD.SOURCE_FROM,
                    DSL.sum(LOAN_RECEIPT_RECHARGE_RECORD.DELIVERY_COUNT).as(LOAN_RECEIPT_RECHARGE_RECORD.DELIVERY_COUNT),
                    DSL.sum(LOAN_RECEIPT_RECHARGE_RECORD.UNIT_PRICE).as(LOAN_RECEIPT_RECHARGE_RECORD.UNIT_PRICE),
                    DSL.sum(LOAN_RECEIPT_RECHARGE_RECORD.CONSUME_AMOUNT).as(LOAN_RECEIPT_RECHARGE_RECORD.CONSUME_AMOUNT),
                    DSL.sum(LOAN_RECEIPT_RECHARGE_RECORD.RECHARGE_AMOUNT).as(LOAN_RECEIPT_RECHARGE_RECORD.RECHARGE_AMOUNT),
                    DSL.sum(LOAN_RECEIPT_RECHARGE_RECORD.BALANCE).as(LOAN_RECEIPT_RECHARGE_RECORD.BALANCE));
            groupBy(LOAN_RECEIPT_RECHARGE_RECORD.DATE);
            //groupBy(LOAN_RECEIPT_RECHARGE_RECORD.SOURCE_FROM);
        }else{
            fields(LOAN_RECEIPT_RECHARGE_RECORD.DATE, LOAN_RECEIPT_RECHARGE_RECORD.SOURCE_FROM, LOAN_RECEIPT_RECHARGE_RECORD.DELIVERY_COUNT,
                    LOAN_RECEIPT_RECHARGE_RECORD.UNIT_PRICE, LOAN_RECEIPT_RECHARGE_RECORD.CONSUME_AMOUNT, LOAN_RECEIPT_RECHARGE_RECORD.RECHARGE_AMOUNT,
                    LOAN_RECEIPT_RECHARGE_RECORD.BALANCE);
        }


        if(searchRequest.getReceiptProductId()== null){
            leftJoin(LOAN_RECEIPT_PRODUCT_INFO, LOAN_RECEIPT_RECHARGE_RECORD.LOAN_RECEIPT_SYSTEM_USER_ID.eq(LOAN_RECEIPT_PRODUCT_INFO.USER_ID)
                    .and(LOAN_RECEIPT_PRODUCT_INFO.STATUS.in(Lists.newArrayList(LoanReceiptProductStatus.ONLINE,LoanReceiptProductStatus.OFFLINE))));
        }else{
            leftJoin(LOAN_RECEIPT_PRODUCT_INFO, LOAN_RECEIPT_RECHARGE_RECORD.LOAN_RECEIPT_SYSTEM_USER_ID.eq(LOAN_RECEIPT_PRODUCT_INFO.USER_ID));
        }


        eq(LOAN_RECEIPT_RECHARGE_RECORD.SOURCE_FROM, searchRequest.getSourceFrom());
        eq(LOAN_RECEIPT_PRODUCT_INFO.ID, searchRequest.getReceiptProductId());
        ge(LOAN_RECEIPT_RECHARGE_RECORD.DATE, searchRequest.getStartDate());
        le(LOAN_RECEIPT_RECHARGE_RECORD.DATE, searchRequest.getEndDate());
        orderBy(LOAN_RECEIPT_RECHARGE_RECORD.DATE, Sort.Direction.DESC);

        Page<ReceiptRechargeStatisticsSearchResponse> response = search(pageable, new RechargeStatisticsSearchResponseRecordMapper());

        if(null == searchRequest.getReceiptProductId()){
            response.forEach(r -> r.setProductName("全部"));
        }

        if(null == searchRequest.getSourceFrom()){
            response.forEach(r -> r.setSourceFrom(null));
        }

        return response;
    }


    private final class RechargeStatisticsSearchResponseRecordMapper implements RecordMapper<Record, ReceiptRechargeStatisticsSearchResponse> {
        @Override
        public ReceiptRechargeStatisticsSearchResponse map(Record record) {
            ReceiptRechargeStatisticsSearchResponse response = new ReceiptRechargeStatisticsSearchResponse();
            response.setDate(record.get(LOAN_RECEIPT_RECHARGE_RECORD.DATE));
            BigDecimal bigDecimal = record.get(LOAN_RECEIPT_RECHARGE_RECORD.DELIVERY_COUNT, BigDecimal.class);
            response.setDeliveryCount(record.get(LOAN_RECEIPT_RECHARGE_RECORD.DELIVERY_COUNT,BigDecimal.class).intValue());
            response.setUnitPrice(record.get(LOAN_RECEIPT_RECHARGE_RECORD.UNIT_PRICE));
            response.setConsumePrice(record.get(LOAN_RECEIPT_RECHARGE_RECORD.CONSUME_AMOUNT, BigDecimal.class).floatValue());
            response.setRechargePrice(record.get(LOAN_RECEIPT_RECHARGE_RECORD.RECHARGE_AMOUNT, BigDecimal.class).floatValue());
            response.setBalance(record.get(LOAN_RECEIPT_RECHARGE_RECORD.BALANCE));
            response.setProductName(record.get(LOAN_RECEIPT_PRODUCT_INFO.NAME));
            response.setProductId(record.get(LOAN_RECEIPT_PRODUCT_INFO.ID));
            response.setSourceFrom(record.get(LOAN_RECEIPT_RECHARGE_RECORD.SOURCE_FROM));
            return response;
        }
    }


}
